package mango.utils;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class ChiperUtils {

    public static byte[] readData(String file) {
        try {
            DataInputStream din = new DataInputStream(new FileInputStream(new File(file)));
            int len = din.available();
            byte[] data = new byte[len];
            din.read(data);
            din.close();
            return data;
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void writeData(String file, byte[] data) {
        try {
            DataOutputStream dout = new DataOutputStream(new FileOutputStream(new File(file)));
            dout.write(data);
            dout.flush();
            dout.close();
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 加密
     * 
     * @param keyfile
     * @param src
     * @param dest
     */
    public static void encrypte(String keyfile, String src, String dest) {
        byte[] srcData = readData(src);
        byte[] rawKeyData = readData(keyfile);
        byte[] destData = MCipher.encrypte(srcData, rawKeyData);
        writeData(dest, destData);
    }

    public static void decrypte(String keyfile, String src, String dest) {
        byte[] srcData = readData(src);
        byte[] rawKeyData = readData(keyfile);
        byte[] destData = MCipher.decrypte(srcData, rawKeyData);
        writeData(dest, destData);
    }

    /**
     * @param keyfile
     * @param cover 是否覆盖
     */
    public static void randomKey(String keyfile, boolean cover) {
        File file = new File(keyfile);
        if (cover || !file.exists()) {
            byte[] data = MCipher.randomKey();
            writeData(keyfile, data);
        }
        else {
            System.err.println(keyfile + " 已存在，没有产生新的密钥！");
        }
    }

    public static void help() {
        System.out.println("创建随机密钥: ");
        System.out.println("    ChiperUtils -genkey keyfile");
        System.out.println("加密: ");
        System.out.println("    ChiperUtils -e keyfile src, dest");
        System.out.println("解密: ");
        System.out.println("    ChiperUtils -d keyfile src dest");
    }

    public static void main(String[] args) {
        try {
            if (args != null && args.length > 0) {
                String type = args[0];
                if ("-genkey".equals(type)) {
                    for (int i = 1; i < args.length; i++) {
                        randomKey(args[i], false);
                    }
                }
                else if ("-e".equals(type)) {
                    if(args.length < 4) {
                        help();
                        return;
                    }
                    File f1 = new File(args[2]);
                    File f2 = new File(args[3]);
                    if(f1.isDirectory()) {
                        if(!f2.exists()) {
                            f2.mkdirs();
                        }
                        if(!f2.isDirectory()) {
                            System.out.println(f2.getPath() + " 不是文件夹");
                            return;
                        }
                        File[] fs = f1.listFiles();
                        if(fs == null) {
                            return;
                        }
                        for(File ff : fs) {
                            encrypte(args[1], ff.getAbsolutePath(), f2.getAbsolutePath() + File.separator + ff.getName());
                        }
                    }
                    else {
                        encrypte(args[1], args[2], args[3]);
                    }
                }
                else if ("-d".equals(type)) {
                    if(args.length < 4) {
                        help();
                        return;
                    }
                    File f1 = new File(args[2]);
                    File f2 = new File(args[3]);
                    if(f1.isDirectory()) {
                        if(!f2.exists()) {
                            f2.mkdirs();
                        }
                        if(!f2.isDirectory()) {
                            System.out.println(f2.getPath() + " 不是文件夹");
                            return;
                        }
                        File[] fs = f1.listFiles();
                        if(fs == null) {
                            return;
                        }
                        for(File ff : fs) {
                            decrypte(args[1], ff.getAbsolutePath(), f2.getAbsolutePath() + File.separator + ff.getName());
                        }
                    }
                    else {
                        decrypte(args[1], args[2], args[3]);
                    }
                }
                else {
                    help();
                }
            }
            else {
                help();
            }
        }
        catch(Exception e) {
            System.err.println(e.toString());
        }
    }
}
